package com.lids.test;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

import java.net.InetSocketAddress;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Des 握手和安全认证
 * @Date: 2021/1/11 16:52
 */
public class LoginAuthRespHandler extends ChannelHandlerAdapter {
    private Map<String,Boolean> nodeCheck = new ConcurrentHashMap<>();
    private String[] whiteList = {"127.0.0.1","192.168.1.115"};

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
           NettyMessage message = (NettyMessage) msg;
           if(message.getHeader()!=null && message.getHeader().getType()==MessageType.LOGIN_REQ.value()){
               String nodeIndex = ctx.channel().remoteAddress().toString();
               NettyMessage loginResp = null;
               //重复登录，拒绝
               if(nodeCheck.containsKey(nodeIndex)){
                   loginResp = buildResponse((byte)-1);
               }else {
                   InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress();
                   String ip = address.getAddress().getHostAddress();
                   boolean isOk = false;
                   for(String WIP:whiteList){
                       if(WIP.equals(ip)){
                           isOk = true;
                       }
                   }
                   loginResp = isOk?buildResponse((byte)0):buildResponse((byte)-1);
                   if(isOk)nodeCheck.put(ip,true);
                   System.out.println("The login response is:"+loginResp+"body["+loginResp.getBody()+"]");
                   ctx.writeAndFlush(loginResp);
               }
           }else {
               ctx.fireChannelRead(msg);
           }
    }

    private NettyMessage buildResponse(byte b){
        NettyMessage message = new NettyMessage();
        Header header = new Header();
        header.setType(MessageType.LOGIN_RESP.value());
        message.setHeader(header);
        message.setBody(b);
        return message;
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        nodeCheck.remove(ctx.channel().remoteAddress().toString());
        ctx.close();
        ctx.fireChannelRead(cause);
    }
}
